using System;
using System.Diagnostics;
using System.IO;
using System.CodeDom.Compiler;
using System.Text;
using System.Collections.Specialized;
using System.Security.Principal;

namespace SvnPmtHook
{
	public class DefaultCaptureConsoleImpl : ICaptureConsole
	{
		private ProcessStartInfo CreateCommandLine(string command)
		{
			ProcessStartInfo startInfo = new ProcessStartInfo();
			startInfo.FileName = "cmd.exe";
			startInfo.Arguments = "/C " + command;
			startInfo.UseShellExecute = false;
			startInfo.RedirectStandardOutput = true;
			startInfo.CreateNoWindow = true;
			return startInfo;
		}

		public string Capture(string command)
		{
			string result = "";
			using (Process process = Process.Start(CreateCommandLine(command)))
			using (StreamReader reader = process.StandardOutput)
			using (StringWriter writer = new StringWriter())
			{
				string outputLine = "";
				while ((outputLine = reader.ReadLine()) != null)
				{
					writer.WriteLine(outputLine);
				}
				result = writer.ToString();
			}
			return result;
		}
	}
}